package com.example.rocketmq.demo.controller;

import com.example.rocketmq.demo.producer.AnnotationProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 基于注解方式的RocketMQ控制器
 */
@Slf4j
@RestController
@RequestMapping("/rocketmq/annotation")
public class AnnotationRocketMQController {

    @Autowired
    private AnnotationProducer annotationProducer;

    /**
     * 发送普通消息
     *
     * @param message 消息内容
     * @param type    发送类型（sync-同步，async-异步，oneway-单向）
     * @return 发送结果
     */
    @GetMapping("/normal")
    public String sendNormal(@RequestParam String message, @RequestParam String type) {
        try {
            switch (type) {
                case "sync":
                    SendResult syncResult = annotationProducer.sendSync(message);
                    return "同步发送成功: " + syncResult.getMsgId();
                case "async":
                    annotationProducer.sendAsync(message);
                    return "异步发送已提交";
                case "oneway":
                    annotationProducer.sendOneWay(message);
                    return "单向发送完成";
                default:
                    return "未知的发送类型: " + type;
            }
        } catch (Exception e) {
            log.error("发送普通消息失败", e);
            return "发送失败: " + e.getMessage();
        }
    }

    /**
     * 发送延时消息
     *
     * @param message    消息内容
     * @param delayLevel 延时等级（1-18）
     * @return 发送结果
     */
    @GetMapping("/delay")
    public String sendDelay(@RequestParam String message, @RequestParam int delayLevel) {
        try {
            SendResult result = annotationProducer.sendDelay(message, delayLevel);
            return "延时消息发送成功: " + result.getMsgId() + ", 延时等级: " + delayLevel;
        } catch (Exception e) {
            log.error("发送延时消息失败", e);
            return "发送失败: " + e.getMessage();
        }
    }

    /**
     * 发送顺序消息
     *
     * @param message 消息内容
     * @param orderId 订单ID
     * @return 发送结果
     */
    @GetMapping("/orderly")
    public String sendOrderly(@RequestParam String message, @RequestParam String orderId) {
        try {
            SendResult result = annotationProducer.sendOrderly(message, orderId);
            return "顺序消息发送成功: " + result.getMsgId() + ", 订单ID: " + orderId;
        } catch (Exception e) {
            log.error("发送顺序消息失败", e);
            return "发送失败: " + e.getMessage();
        }
    }

    /**
     * 发送带标签的过滤消息
     *
     * @param message 消息内容
     * @param tag     消息标签
     * @return 发送结果
     */
    @GetMapping("/filter/tag")
    public String sendWithTag(@RequestParam String message, @RequestParam String tag) {
        try {
            SendResult result = annotationProducer.sendWithTag(message, tag);
            return "带标签的过滤消息发送成功: " + result.getMsgId() + ", 标签: " + tag;
        } catch (Exception e) {
            log.error("发送带标签的过滤消息失败", e);
            return "发送失败: " + e.getMessage();
        }
    }

    /**
     * 发送带属性的SQL过滤消息
     *
     * @param message 消息内容
     * @param age     年龄属性
     * @return 发送结果
     */
    @GetMapping("/filter/sql")
    public String sendWithProperty(@RequestParam String message, @RequestParam int age) {
        try {
            SendResult result = annotationProducer.sendWithProperty(message, age);
            return "带属性的SQL过滤消息发送成功: " + result.getMsgId() + ", 年龄: " + age;
        } catch (Exception e) {
            log.error("发送带属性的SQL过滤消息失败", e);
            return "发送失败: " + e.getMessage();
        }
    }

    /**
     * 发送事务消息
     *
     * @param message 消息内容
     * @param status  事务状态（COMMIT, ROLLBACK, UNKNOWN）
     * @return 发送结果
     */
    @GetMapping("/transaction")
    public String sendTransaction(@RequestParam String message, @RequestParam String status) {
        try {
            TransactionSendResult result = annotationProducer.sendTransaction(message, status);
            return "事务消息发送成功: " + result.getMsgId() + ", 本地事务状态: " + status;
        } catch (Exception e) {
            log.error("发送事务消息失败", e);
            return "发送失败: " + e.getMessage();
        }
    }
}